/**     
 * @Title: UserController.java   
 * @Package me.api2doc.controller   
 * @Description: TODO
 * @author weiwei 
 * @date 2018年3月29日 上午10:41:19   
 * @version V1.0     
 */
package me.api2doc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import javax.annotation.PostConstruct;

import me.api2doc.vo.UserVo;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.terran4j.commons.api2doc.annotations.Api2Doc;
import com.terran4j.commons.api2doc.annotations.ApiComment;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @author weiwei
 * @date 2018年3月29日 上午10:41:19
 * 
 */
@Api2Doc(value = "user", name = "用户相关接口")
@RestController
@RequestMapping(value = "/v1/user")
public class UserController {

	private AtomicLong idCount = new AtomicLong(0);

	private Map<Long, UserVo> users = new ConcurrentHashMap<Long, UserVo>();

	@PostConstruct
	public void init() {
		registUser("xaoyaoyao1");
		registUser("xaoyaoyao2");
		registUser("xaoyaoyao3");
	}

	@ApiComment("根据指定的用户名称，注册一个新用户")
	@RequestMapping(value = "", method = RequestMethod.POST, name = "注册新用户")
	public UserVo registUser(String name) {
		long id = idCount.incrementAndGet();
		UserVo user = new UserVo(id, name);
		users.put(id, user);
		return user;
	}

	@RequestMapping(value = "", method = RequestMethod.GET, name = "获取用户列表")
	public List<UserVo> getUsers() {
		return new ArrayList<UserVo>(users.values());
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET, name = "获取用户详细信息")
	public UserVo getUser(@PathVariable Long id) {
		return users.get(id);
	}

	@ApiComment("更新指定用户的用户名称")
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, name = "更新用户名")
	public void putUser(@PathVariable Long id, String name) {
		users.get(id).setName(name);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE, name = "删除用户")
	public void deleteUser(@PathVariable Long id) {
		users.remove(id);
	}
}
